package com.ywicc.usblibs;

import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.OnLifecycleEvent;

import com.ywicc.usblibs.utils.UsbUtils;

import java.util.Arrays;

/**
 * author : zhouxuan77
 * e-mail : zhouxuan77@163.com
 * time   : 2022/03/21
 * desc   : USB Service 实现
 * version: 1.0
 */
public class UsbService extends LifecycleService {

    public static boolean isUsbServiceStart = false;

    private final static String TAG = "UsbService";

    private final UsbUtils usbUtils;

    private OnUsbRecvDataListener onUsbRecvDataListener;
    private int intervalTime;

    public UsbService() {
        getLifecycle().addObserver(new UsbObserver());

        usbUtils = new UsbUtils(UsbService.this);

        Log.w(TAG, "UsbService Init");
    }

    /**
     * 注册 USB 数据接收
     *
     * @param onUsbRecvDataListener UsbService.OnUsbRecvDataListener 对象
     * @param intervalTime          返回数据间隔时长 (多久获取一次数据)
     */
    public void registerListener(OnUsbRecvDataListener onUsbRecvDataListener, int intervalTime) {
        Log.v(TAG, "registerListener");

        this.intervalTime = intervalTime;

        this.onUsbRecvDataListener = onUsbRecvDataListener;
    }

    /**
     * 注销 USB 数据接收
     */
    public void destroyListener() {
        Log.v(TAG, "destroyListener");

        this.onUsbRecvDataListener = null;
    }

    /**
     * 通过 USB 发送数据
     *
     * @param data 需要发送的数据 (byte 数组)
     */
    public void sendData(byte[] data) {
        Log.w(TAG, "sendData: " + Arrays.toString(data));

        usbUtils.sendData(data);
    }

    @Nullable
    @Override
    public IBinder onBind(@NonNull Intent intent) {
        super.onBind(intent);
        return new UsbBinder();
    }

    class UsbObserver implements LifecycleObserver {

        @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
        public void startService() {
            usbUtils.initUsb(new UsbUtils.OnUsbDataResultListener() {
                @Override
                public void callBackUsbRecvData(byte[] data) {
                    Log.v(TAG, "Recv Data：" + Arrays.toString(data));

                    if (onUsbRecvDataListener != null) {
                        onUsbRecvDataListener.callBackRecvData(data);
                    }
                }
            }, intervalTime);

            isUsbServiceStart = true;

            Log.w(TAG, "startService");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        public void stopService() {
            isUsbServiceStart = false;

            usbUtils.destroyListener();

            Log.w(TAG, "stopService");
        }
    }

    public class UsbBinder extends Binder {
        public UsbService getService() {
            return UsbService.this;
        }
    }

    public interface OnUsbRecvDataListener {
        void callBackRecvData(byte[] data);
    }
}
